﻿using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunArts.EasyDevOps.Rcl.Utils
{
    public static class SftpExtensions
    {
        public static void CreateDirectoryRecursive(this SftpClient client, string directoryPath)
        {
            if (client == null)
                throw new ArgumentNullException(nameof(client));

            if (string.IsNullOrWhiteSpace(directoryPath))
                throw new ArgumentException("目录路径不能为空", nameof(directoryPath));

            // 规范化路径（确保使用正确的目录分隔符）
            directoryPath = directoryPath.Replace('\\', '/').TrimEnd('/');

            // 检查目录是否已存在
            if (client.Exists(directoryPath))
                return;

            // 获取父目录路径
            var parentDirectory = Path.GetDirectoryName(directoryPath)?.Replace('\\', '/');

            // 递归创建父目录（如果不存在）
            if (!string.IsNullOrEmpty(parentDirectory) && !client.Exists(parentDirectory))
            {
                CreateDirectoryRecursive(client, parentDirectory);
            }

            // 创建当前目录
            client.CreateDirectory(directoryPath);
        }
    }
}
